<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="/common/taglibs.jsp"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>    
    
    <title>代码设置列表</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
	<link rel="shortcut icon" href="${ctx }/ico/favicon.ico"> 
	<link href="${ctx }/css/bootstrap.min.css?v=3.3.6" rel="stylesheet">
    <link href="${ctx }/css/font-awesome.css?v=4.4.0" rel="stylesheet">
    <link href="${ctx }/css/common.css" rel="stylesheet">

    <!-- jqgrid-->
    <link href="${ctx }/css/plugins/jqgrid/ui.jqgrid.css?0820" rel="stylesheet">

    <link href="${ctx }/css/animate.css" rel="stylesheet">
    <link href="${ctx }/css/style.css?v=4.1.0" rel="stylesheet">
    <!-- Sweet Alert -->
    <link href="${ctx }/css/plugins/sweetalert/sweetalert.css" rel="stylesheet">
  </head>
  
  <body class="gray-bg">
    <div class="animated fadeInRight">
        <div class="row">
            <div class="col-sm-12">
                <div class="ibox ">
                    <div class="ibox-content">
                    	<div class="row m-b-sm m-t-sm">
	                    	<div class="col-md-11">
	                    		<div class="input-group">
		                    		<button type="button" class="btn btn-success btn-outline" id="goBack"><i class="fa fa-reply"></i> 返回上一层</button>
		                    		<button class="btn btn-success btn-outline commonbtn" type="button" id="addSysCode"><i class="fa fa-plus"></i> 添加</button>                    		
		                    		<button class="btn btn-success btn-outline commonbtn" type="button" id="editSysCode"><i class="fa fa-pencil"></i> 编辑</button>
		                    		<button class="btn btn-success btn-outline commonbtn" type="button" id="delSysCode"><i class="fa fa-trash"></i> 删除</button>
		                            <input type="text" placeholder="请输入代码值名称" class="input-sm form-control" style="float: right;height: 100%;width: 50%" id="search"> 
		                            <span class="input-group-btn"><button type="button" class="btn btn-sm btn-primary commonsearch" id="searchBtn"> 搜索</button> </span>
		                            
	                            </div>
	                        </div>
                    	</div>
                    	
                    	<div class="jqGrid_wrapper">
                            <table id="table_list"></table>
                            <div id="pager_list"></div>
                        </div>
                    </div>                    
                 </div>
             </div>
         </div>
     </div>
  	
  	
  	<!-- 添加菜单模态窗口 -->
  	 <div class="modal inmodal" id="addSysCodeWin" tabindex="-1" role="dialog" aria-hidden="true">
    	<div class="modal-dialog">
        	<div class="modal-content animated bounceInRight">
            	<div class="modal-header">
                	<button type="button" class="close closeWin" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">关闭</span>
                    </button>                 	
                 	<h5 class="modal-title">添加代码</h5>                  	
                </div>
                
                <div class="ibox-content">
                        <form class="form-horizontal m-t" id="addSysCodeForm">
                            <input type="hidden" id="theTypeName" value="${typeName} "/>
                            <div class="form-group">
                                <label class="col-sm-3 control-label">代码名称：</label>
                                <div class="col-sm-8">
                                    <input id="codeName" name="codeName" class="form-control" required="" aria-required="true" type="text">                                    
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-3 control-label">代码值：</label>
                                <div class="col-sm-8">
                                    <input id="codeValue" name="codeValue" class="form-control" type="text" required="" aria-required="true">                                    
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-3 control-label">代码描述：</label>
                                <div class="col-sm-8">
                                    <input id="codeDesc" name="codeDesc" class="form-control" type="text" required="" aria-required="true">                                    
                                </div>
                            </div>
                            <div class="form-group">
                            	<label class="col-sm-3 control-label">显示顺序：</label>
                            	<div class="col-sm-8">
                                    <input id="showOrder" name="showOrder" class="form-control"  type="number" required="" aria-required="true">                                    
                                </div>
							</div>
                            <div class="form-group">
                                <div class="col-sm-8 col-sm-offset-3">                                   
                                    <button type="button" class="btn btn-white" data-dismiss="modal">关闭</button>
                					<button type="submit" class="btn btn-primary" id="add_SysCode_submit">保存</button>
                                </div>
                            </div>
                        </form>
               	</div>
            	
        	</div>
        </div>
  	</div> 
  	<!--end 添加代码模态窗口 -->
  	
  	
  	<!-- 修改代码模态窗口 -->
  	<div class="modal inmodal" id="editSysCodeWin"  role="dialog" aria-hidden="true">
                     <iframe id="editSysCodeFrame" width="100%" height="100%" frameborder="0" ></iframe>
              
  	</div>
	<!--end 修改菜单模态窗口 -->
                        
  	<!-- 全局js -->
    <script src="${ctx }/js/jquery.min.js?v=2.1.4"></script>
    <script src="${ctx }/js/bootstrap.min.js?v=3.3.6"></script>

    <!-- Peity -->
    <script src="${ctx }/js/plugins/peity/jquery.peity.min.js"></script>

    <!-- jqGrid -->
    <script src="${ctx }/js/plugins/jqgrid/i18n/grid.locale-cn.js?0820"></script>
    <script src="${ctx }/js/plugins/jqgrid/jquery.jqGrid.min.js?0820"></script>
    
    <!-- jQuery Validation plugin javascript-->
    <script src="${ctx }/js/plugins/validate/jquery.validate.min.js"></script>
    <script src="${ctx }/js/plugins/validate/messages_zh.min.js"></script>

	<!-- layer javascript -->
	<script src="${ctx }/js/plugins/layer/layer.min.js"></script>
	
	<!-- jQuery form-->
	<script src="${ctx }/js/jquery.form.js"></script>
	
	
    <!-- 自定义js -->
    <script src="${ctx }/js/content.js?v=1.0.0"></script>
    <!-- Sweet alert -->
    <script src="${ctx }/js/plugins/sweetalert/sweetalert.min.js"></script>

    <script>
		$(document).ready(function () {
			$.jgrid.defaults.styleUI = 'Bootstrap';
	        // Examle data for jqGrid	        
			var theTypeName=$('#theTypeName').val();
			//绑定表格数据
			$("#table_list").jqGrid({	        	
		    	url:"${ctx }/code/loadDataGrid?typeName="+theTypeName,		        	
		     	datatype: "json",
		     	height: "calc(100% - 265px)",
		      	autowidth: true,
		     	shrinkToFit: true,
		     	multiselect: true,
		      	rowList: [10, 20, 30],
		      	root:"rows",
			    colNames: ['代码名称','代码值', '代码描述', '显示顺序'],
		      	colModel: [
		      
		     	{
					name: 'codeName',	            
		            width: '25%'	           
		      	},
		        {
		        	name: 'codeValue',
		        	width: '20%'
		        },
		        {
		         	name: 'codeDesc',
		            width: '26%',
		            	           
		        },
		       	{
		         	name: 'showOrder',
		            width: '24%',
		           		           
		        }],
		        sortable:true,
		        sortname: 'showOrder',
		        sortorder: 'asc',
				pager: "#pager_list",
	            caption: "代码值列表",
            });
        });
        
         // jqGrid 自动调整宽度
        $(window).bind('resize', function () {
        	var width = $('.jqGrid_wrapper').width();
       		$('#table_list').setGridWidth(width);       		
     	});
        
        //绑定查询事件
        $("#searchBtn").click(function(){
        	var theTypeName=$('#theTypeName').val();
        	var searchString = $("#search").val(); 
	        $("#table_list").jqGrid('setGridParam',{ 
	            url:"${ctx }/code/loadDataGrid?typeName="+theTypeName, 
	            postData:{'codeName':searchString}, //发送数据 
	            page:1
	        }).trigger("reloadGrid"); //重新载入 
    	});
    	
		
        //绑定add表单提交事件,异步提交表单
        $('#addSysCodeForm').submit(function(){
        	var theTypeName=$('#theTypeName').val();
			var fromUrl="${ctx }/code/addSysCode?typeName="+theTypeName;
        	$.ajax({
		    	type: "post",
		    	url: fromUrl,
		     	data: $('#addSysCodeForm').serialize(),
		     	success: function(data) {
		      		if(data.errcode=="0")
		      		{
		      			layer.alert(data.errmsg,{icon: 1}, function(index){		      			
		      				//刷新表格数据
							$("#table_list").trigger("reloadGrid");	
							
							//清空表单数据
							$("#addSysCodeForm :input").not(":button, :submit, :reset, :hidden").val("").removeAttr("checked").remove("selected");
							
							//关闭add窗口
							$('#addSysCodeWin').modal('hide');
							//关闭提示框
							layer.close(index);
						});		      			
		      		}else if(data=="1"){
		      			layer.alert(data.errmsg,{icon: 2});
		      		}else if(data=="2"){
		      			layer.alert('添加代码失败！代码名称不能为空',{icon: 2});
		      		}
		      	},
		      	error: function() {		      		
		      		layer.alert('添加代码失败！',{icon: 2});
        		}
        	});
        	return false;
        });
        
        //绑定修改按钮事件
        $('#addSysCode').click(function(){
        	$('#addSysCodeWin').modal('show');
        });
        
        //绑定修改按钮事件
        $('#editSysCode').click(function(){
        	var theTypeName=$('#theTypeName').val();
        	var selectedId = $("#table_list").jqGrid("getGridParam", "selrow");
        	var theCode=$("#table_list").jqGrid("getRowData",selectedId);
        	if(selectedId==null||selectedId=="")
			{
				layer.alert('请选择一项！',{icon: 0});
				return;
			}
			//alert(theCode.codeName);
			var frameSrc ="${ctx}/code/editSysCode?typeName="+theTypeName+"&codeName="+encodeURIComponent(theCode.codeName);
			$("#editSysCodeFrame").attr("src", frameSrc);
        	$('#editSysCodeWin').modal('show');
        });
        
        function editWinClose(flag)
        {
        	if(flag=="0")
        	{
        		$("#table_list").trigger("reloadGrid");
        	}
        	
			//关闭edit窗口
			$('#editSysCodeWin').modal('hide');
        }
        
        $('#goBack').click(function(){
        location.href="${ctx}/codeType/index";
        
        });
        
        
        //绑定删除事件
        $('#delSysCode').click(function(){   
        	var theTypeName=$('#theTypeName').val();     	
			var selectedIds = $("#table_list").jqGrid("getGridParam", "selarrrow");
			//alert("selectedIds:"+selectedIds);
			var theCodeNames="";
			for(var i=0;i<selectedIds.length;i++){
			
			var code=$("#table_list").jqGrid("getRowData",selectedIds[i]);
			//console.log(codeType);
			if(theCodeNames != null && theCodeNames != "")
			theCodeNames+=",";
			theCodeNames=theCodeNames+code.codeName;
			
			}
			
			if(selectedIds=="")
			{
				layer.alert('请选择一项！',{icon: 0});
				return;
			}
			swal({
				title: "您确定要删除这条信息吗",
				text: "删除后将无法恢复，请谨慎操作！",
				type: "warning",
				showCancelButton: true,
				confirmButtonColor: "#DD6B55",
				confirmButtonText: "删除",
				closeOnConfirm: false
				}, function () {
				$.ajax({
					type: "post",
					dataType:"json",
					url: "${ctx }/code/deleteByCodeName?typeName="+theTypeName,
					data: "&codeNames="+encodeURIComponent(theCodeNames),
					success: function(data) {
						if(data.errcode=="0")
	   					{
	   						//刷新表格数据
							$("#table_list").trigger("reloadGrid");
							swal("删除成功！", "您已经永久删除了这条信息。", "success");	
						}else
						{
							swal("删除用户失败！", "您未删除这条信息。", "error");
						}
	 				},
					error: function() {
						swal("删除用户失败！", "您未删除这条信息。", "error");
					}
				});
			});
        });

    </script>
  </body>
</html>
